package ir;

import java.util.Date;
import java.util.GregorianCalendar;


/**
 * Special case for handling of java.util.Calendar objects, which
 * must be converted into java.util.Date objects because
 * GWT does not support emulation of Calendar.
 * 
 * @author tchau@google.com (Your Name Here)
 *
 */
public class CalendarFieldNode extends FieldNode {

  public CalendarFieldNode(Class<?> c, String fieldname) {
    super(c, fieldname);
  }
  
  @Override
  public String gen() {
    return INDENT + "private Date " + getName() + ";\n";
  }

  @Override
  public String getTypeName() {
    return "Date";
  }
  
  @Override
  public String genGwt2Soap() {
    
      // Convert the Date to calendar:
    /*
     * input: Date in.get<Fieldname>
     * output: out.set( <fieldname_cal> )
     * 
     * Calendar <fieldname>_cal = new GregorianCalendar();
     * <fieldname>_cal.setTime(in.get<fieldname>);
     * out.set<fieldname>(<fieldname_cal>);
     */
      String conversion = "";
      String cal_name = getName() + "_cal";
      String cal_declare = "Calendar " + cal_name + " = new GregorianCalendar();\n";
      String cal_setTime = cal_name + ".setTime(in.get" + getName() + "());\n";
      String glue = "out.set" + getName() + "(" + cal_name + ");\n";
      
      conversion = cal_declare + cal_setTime + glue;
      return conversion;
//      return "out.set" + getName() + "( Translator.instance().Gwt2Soap(in.get" + getName() + "() ));\n";
  }

  @Override
  public String genSoap2Gwt() {
    
      /**
       * Input: Calendar in.get<fieldname>
       * out: Date
       * 
       * out.set<Fieldname>( in.get<fieldname>.gettime());
       * 
       */
    String getDate = "in.get" + getName() + "()";
      return "out.set" + getName() + "( (null =="+ getDate + " )?null:" + getDate + ".getTime());\n";
//      return "out.set" + getName() + "( Translator.instance().Gwt2Soap(in.get" + getName() + "() ));\n";
  }

  
}
